package com.xt.purchasingsystem.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xt.purchasingsystem.dto.BuyerPlanDto;
import com.xt.purchasingsystem.mapper.BuyerPlanDetailMapper;
import com.xt.purchasingsystem.mapper.BuyerPlanMapper;
import com.xt.purchasingsystem.model.BuyerPlan;
import com.xt.purchasingsystem.model.BuyerPlanDetail;
import com.xt.purchasingsystem.service.BuyerPlanService;
import com.xt.purchasingsystem.utils.IdWorker;
import com.xt.purchasingsystem.utils.Result;
import com.xt.purchasingsystem.utils.ResultFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 采购计划单所有业务实现类
 * </p>
 *
 * @author yhp
 * @since 2020-05-31
 */
@Service
@Transactional
public class BuyerPlanServiceImpl extends ServiceImpl<BuyerPlanMapper, BuyerPlan> implements BuyerPlanService {
    @Autowired
    private IdWorker idWorker;
    @Autowired
    private BuyerPlanMapper buyerPlanMapper;
    @Autowired
    private BuyerPlanDetailMapper buyerPlanDetailMapper;

    @Override
    public Result saveBuyerPlan(BuyerPlanDto buyerPlanDto) {
        String id = String.valueOf(idWorker.nextId());
        buyerPlanDto.getBuyerPlan().setId(id);
        buyerPlanMapper.insert(buyerPlanDto.getBuyerPlan());
        for (BuyerPlanDetail buyerPlanDetail : buyerPlanDto.getBuyerPlanDetailList()) {
            buyerPlanDetail.setId(String.valueOf(idWorker.nextId()));
            buyerPlanDetail.setBuyerPlanId(id);
            buyerPlanDetailMapper.insert(buyerPlanDetail);
        }
        return ResultFactory.buildSuccessResult("添加成功,需要审核!");
    }

    @Override
    public Result getAllBuyerPlanNoCheck(Integer currNo, Integer pageSize) {
        PageHelper.startPage(currNo,pageSize);
        List<BuyerPlanDto> allBuyerPlanNoCheck = buyerPlanMapper.getAllBuyerPlanNoCheck();
        PageInfo<BuyerPlanDto> pageInfo=new PageInfo<>(allBuyerPlanNoCheck);
        return ResultFactory.buildSuccessResult("查询成功",pageInfo);
    }
}
